package com.dahua.messagesyncsh.business.mitsubishi.http;


import lombok.Getter;
import lombok.Setter;

import java.text.MessageFormat;

/**
 * CommonException：通用异常
 * 2021/12/10
 *
 * @author smec
 */
@Getter
@Setter
public class CommonException extends RuntimeException {
    private String code;
    private String message;
    private ResultEnum resultEnum;

    /**
     * 默认code=ResultEnum.FAIL.getCode()
     *
     * @param msg message
     */
    public CommonException(String msg) {
        code = ResultEnum.FAIL.getCode();
        this.message = msg;
    }

    /**
     * 异常参数初始化方法
     *
     * @param code    code
     * @param message message
     */
    public CommonException(String code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 异常枚举初始化方法
     *
     * @param resultEnum 返回枚举
     */
    public CommonException(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMessage();
        this.resultEnum = resultEnum;
    }

    /**
     * 异常枚举初始化方法
     *
     * @param resultEnum
     */
    public CommonException(ResultEnum resultEnum, Object[] args) {
        this.code = resultEnum.getCode();
        this.message = MessageFormat.format(resultEnum.getMessage(), args);
        this.resultEnum = resultEnum;
    }
}
